/**
 * 后台题目管理
 */
const express = require('express')
const article = require('../../middleware/article')
const category = require('../../middleware/category')
const teacher = require('../../middleware/teacher')
const articleApp = express()

// 加载列表页
articleApp.get('/', article.getCount, (req, res, next) => {
    let { articleCount } = req

    let size = 5 // 每页显示5条
    req.page = {}
    req.page.count = articleCount
    req.page.total = Math.ceil(req.page.count / size)
    req.page.p = req.query.p ? req.query.p : 1
    req.page.p = req.page.p > req.page.total ? req.page.total : req.page.p
    req.page.p = req.page.p < 1 ? 1 : req.page.p

    res.start = (req.page.p - 1) * size
    res.size = size

    next()

}, [article.getPage, category.getList], (req, res) => {
    let { user, pageList, page, categories } = req
    let { category_id, hot, code } = req.query

    page.list = pageList
    res.render('admin/article/index', { user: user, page: page, categories: categories, category_id: category_id, hot: hot, code: code })
})

articleApp.get('/my/:tid',[article.getListByTid,article.getArticleCount],(req,res)=>{
    let {articles,user,myArticleCount} = req
    res.render('admin/my', {articles:articles,user:user,myArticleCount:myArticleCount})
})

// 设置热门推荐
articleApp.get('/sethot', article.setHot, (req, res) => {
    if (req.affectedRows > 0) {
        res.json({ code: 1, msg: '设置成功' })
    } else {
        res.json({ code: 0, msg: '设置失败' })
    }
})

// 显示添加选题页
articleApp.get('/add', [category.getList, teacher.getList], (req, res) => {
    let { user, categories, teachers } = req
    res.render('admin/article/add', { user: user, categories: categories, teachers: teachers, code: '' })
})
// 添加题目
articleApp.post('/add', [article.add, category.getList, teacher.getList], (req, res) => {
    let { user, categories, teachers } = req
    if (req.insertId) {
        res.render('admin/article/add', { user: user, categories: categories, teachers: teachers, code: 1 })
    } else {
        res.render('admin/article/add', { user: user, categories: categories, teachers: teachers, code: 2 })
    }
})

// ckeditor 上传
articleApp.post('/ckeditor', (req, res) => {
    if (req.uploadUrl) {
        res.json({
            uploaded: true,
            url: req.uploadUrl
        })
    } else {
        res.json({
            uploaded: false,
            err: { message: '上传失败' }
        })
    }
})


// 删除题目
articleApp.get('/del', article.del, (req, res) => {
    if (req.affectedRows > 0) {
        res.json({ code: 1, msg: '删除成功' })
    } else {
        res.json({ code: 2, msg: '删除失败' })
    }
})

// 打开编辑页面
articleApp.get('/edit/:id', [category.getList, article.getArticleById, teacher.getList,article.getStudentsList], (req, res) => {
    let { user, categories, article, teachers, students } = req
    res.render('admin/article/edit', { user: user, categories: categories, article: article, teachers: teachers, students: students, code: '' })
})
// 所有题目编辑
articleApp.post('/edit', article.edit, (req, res) => {
    if (req.affectedRows > 0) {
        res.render('admin/alert', { code: true, title: '成功提示', message: '题目编辑成功', url: '/admin/article/' })
    } else {
        res.render('admin/alert', { code: true, title: '失败提示', message: '题目编辑失败', url: '/admin/article/edit/' + req.body.id })
    }
})
// 个人题目编辑
articleApp.get('/my/edit/:id', [category.getList, article.getArticleById, teacher.getList,article.getStudentsList], (req, res) => {
    let { user, categories, article, teachers, students } = req
    res.render('admin/my/edit', { user: user, categories: categories, article: article, teachers: teachers, students: students, code: '' })
})
articleApp.post('/my/edit', article.edit, (req, res) => {
    if (req.affectedRows > 0) {
        res.render('admin/alert', { code: true, title: '成功提示', message: '题目编辑成功', url: '/admin/article/my/' + req.body.tid })
    } else {
        res.render('admin/alert', { code: true, title: '失败提示', message: '题目编辑失败', url: '/admin/article/my/edit/' + req.body.id })
    }
})
// 反选学生
articleApp.post('/my/setStudent', article.setStudent, (req, res) => {
    if (req.affectedRows > 0) {
        res.render('admin/alert', { code: true, title: '成功提示', message: '操作成功', url: '/admin/article/my/' + req.body.tid })
    } else {
        res.render('admin/alert', { code: true, title: '失败提示', message: '操作失败', url: '/admin/article/my/edit/' + req.body.id })
    }
})

module.exports = articleApp